package com.addressbook.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Random;

import org.testng.annotations.Test;

import com.addressbook.fw.ContactData;
import com.addressbook.utils.SortedListOf;

public class ContactDeletionTests extends TestBase {

	@Test
	public void testContactDeletion() {
		// save old
		SortedListOf<ContactData> oldstate = new SortedListOf<ContactData> (app.getHibernateHelper().listContacts());
		if (oldstate.size() == 0)
			throw new Error("There is no contacts to Delete.");
		Random rnd = new Random();
		int index = rnd.nextInt(oldstate.size());

		app.getContactHelper().deleteContactByindex(index);

		// save new
		for (ContactData contactData : oldstate) {
			contactData.correctionContact();
		}
		SortedListOf<ContactData> newstate = app.getContactHelper().getContacts();

		// check
		assertThat(newstate, equalTo(oldstate.without(index)));
	}
}
